import path from 'path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Pages from 'vite-plugin-pages'
import Legacy from '@vitejs/plugin-legacy'
import Layouts from 'vite-plugin-vue-layouts'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import VueI18n from '@intlify/vite-plugin-vue-i18n'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  plugins: [
    Vue({
      include: [/\.vue$/],
      reactivityTransform: true,
    }),
    Legacy({
      targets: ['defaults', 'not IE 11'],
      modernPolyfills: true,
    }),
    Pages({
      dirs: [{ dir: 'src/pages', baseRoute: '/' }],
      extensions: ['vue'],
      exclude: ['**/components/*.*'],
    }),
    Layouts({ defaultLayout: 'default' }),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        'vue-i18n',
        'vue/macros',
        '@vueuse/head',
        '@vueuse/core',
      ],
      dts: 'src/auto-imports.d.ts',
      dirs: [
        'src/store',
      ],
      vueTemplate: true,
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      extensions: ['vue'],
      include: [/\.vue$/, /\.vue\?vue/],
      dts: 'src/components.d.ts',
      resolvers: [ElementPlusResolver()],
    }),
    VueI18n({
      runtimeOnly: true,
      compositionOnly: true,
      include: [path.resolve(__dirname, 'locales/**')],
    }),
  ],
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          hack: `true; @import (reference) "${path.resolve('src/assets/style/variables.less')}";`,
        },
        javascriptEnabled: true,
      },
    },
  },
  server: {
    port: 9009,
  },
  build: {
    target: 'es2015',
  },
})
